#include "main/utils.h"
using namespace std;

int divided(int dividend, int divisor) {
  // 溢出
  if (dividend == 0x80000000 && divisor == -1)
    return INT_MAX;

  int negative = 2;
  if (dividend > 0) {
    --negative;
    dividend = -dividend;
  }
  if (divisor > 0) {
    --negative;
    divisor = -divisor;
  }

  int result = 0;
  while (dividend <= divisor) {
    int cur_divisor = divisor;
    int quotient = 1;
    while (cur_divisor >= 0xc0000000 && dividend <= cur_divisor + cur_divisor) {
      quotient += quotient;
      cur_divisor += cur_divisor;
    }
    dividend -= cur_divisor;
    result += quotient;
  }

  return negative == 1 ? -result : result;
}

int main() {
  cout << "7/2=" << divided(7, 2) << endl;
  cout << "-7/2=" << divided(-7, 2) << endl;
  cout << "7/-2=" << divided(7, -2) << endl;
  cout << "-7/-2=" << divided(-7, -2) << endl;

  return 0;
}
